<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/27.
 */

namespace App\Http\Controllers\Traits;

use App\Utils\ResultsVo;

trait StoreTrait
{
    public function store(): \Illuminate\Http\JsonResponse
    {
        $data = request()->all();
        if (method_exists($this, 'validated')) {
            $this->validated();
        }
        if (method_exists($this, 'before')) {
            $data = $this->before($data);
        }

        $model = $this->getModel();
        $dao = modelBuild($model);
        if (method_exists($dao, 'defaultValue')) {
            $dao->defaultValue();
        }

        $dao->fill($data)->save();

        if (method_exists($this, 'after')) {
            $this->after($dao);
        }

        return ResultsVo::success();
    }
}
